//
//  NetRequest.m
//  
//
//  Created by MMC on 13-10-7.
//
//

#import "NetRequest.h"
#import "GlobalVar.h"
@implementation NetRequest
@synthesize delegate;

-(void)getNetWork:(NSString *)url CanShu:(NSString *)Can
{
    NSURL *URL = [NSURL URLWithString:url];
	//NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:URL];
    NSMutableURLRequest* theRequest=[NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0f];
    
	//以下对请求信息添加属性前四句是必有的，第五句是soap信息。
	[theRequest addValue: @"text/xml;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  
    
    [theRequest setValue:SOFT_ID forHTTPHeaderField:@"mcid"];
    [theRequest setValue:SOFT_ID forHTTPHeaderField:@"product"];
    [theRequest setValue:@"iphone" forHTTPHeaderField:@"platform"];
        
    NSString *Tel = @"";
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"LOGIN_USER_KEY"]){
        Tel = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"LOGIN_USER_KEY"]];
        if([Tel length] > 0){
            [theRequest setValue:Tel forHTTPHeaderField:@"msisdn"];
        }else{
            [theRequest setValue:@"" forHTTPHeaderField:@"msisdn"];
        }
    }else{
        [theRequest setValue:@"" forHTTPHeaderField:@"msisdn"];
    }
    
    if([[NSUserDefaults standardUserDefaults] objectForKey:@"TempPhoto"])
    {
        NSString *Phone = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"TempPhoto"]];
        if([Phone length] > 0){
            [theRequest setValue:Phone forHTTPHeaderField:@"msisdn"];
        }
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"TempPhoto"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    [theRequest setHTTPBody:[Can dataUsingEncoding:NSUTF8StringEncoding]];
    [theRequest setHTTPMethod:@"POST"];
    
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
	
	if (theConnection) {
        
		NSLog(@"链接成功");
		receivedData=[[NSMutableData data] retain];
	} else {
		NSLog(@"链接失败");
		
	}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	
	[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[receivedData appendData:data];
	
	
	//重新加載xmlParser
	if( SwXml )
	{
		[SwXml release];
        SwXml = nil;
	}
	
	//[connection release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
    [connection cancel];
    //[connection release];
	[receivedData release];
    
    
    if (delegate) {
        [delegate Delegate_FeedBackFail:self value:@"网络不通或者连接超时"];
    }
    // NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	
	NSString *theXML = [[NSString alloc] initWithBytes: [receivedData mutableBytes] length:[receivedData length] encoding:NSUTF8StringEncoding];
    //自己加上去的方法
    NSLog(@"theXML is:%@",theXML);
    if([theXML length] > 0){
        if(delegate){
            if([delegate respondsToSelector:@selector(Delegate_FeedBack:value:)]){
                [delegate Delegate_FeedBack:self value:theXML];
            }
        }
    }
    
//    NSData *Data = [theXML dataUsingEncoding:NSUTF8StringEncoding];
//	
//	SwXml = [[NSXMLParser alloc] initWithData:Data];
//	[SwXml setDelegate: self];
//	[SwXml setShouldResolveExternalEntities: YES];
//	[SwXml parse];
//	[connection release];
//	[receivedData release];
    [theXML release];
}

#pragma mark 解析数据
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
   attributes: (NSDictionary *)attributeDict
{
	//NSLog(@"4 parser didStarElemen: namespaceURI: attributes:");
    
	if( [elementName isEqualToString:@"course"])
	{
		
		
        SwMuString = [[NSMutableString alloc] init];
        
		recordResults = YES;
	}
    
	
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	//NSLog(@"5 parser: foundCharacters:");
	
	if( recordResults )
	{
		//NSLog(@"string::::%@",[string JSONValue]);
		[SwMuString  appendString: string];
		
		
	}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    
    [SwMuString release];
    SwMuString = nil;
    SwMuString = [[NSMutableString alloc] init];
    
    
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
	NSLog(@"-------------------start--------------");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
	NSLog(@"-------------------end--------------");

}
@end
